28 October 2017

R to visualize your data

Why visualize your data?


  • summarize information
  • check assumptions validity
  • many pieces of information at one glance
  • ease the understanding

Why use R to do it?

Workflow



         

Why use R to do it?

Workflow



           

Why use R to do it?

Here we'll argue why use R for doing your plots:

  • visualizing data
  • what makes a good graph (some details)
  • work-flow (data are already in R)
  • base graph / ggplot2 / web graphs (interacting)
  • code / reproducible

Why use R to do it?

Different valuable options

  • The gridGraphics package
  • base VS ggplot2
  • Web-based graphs

Why use R to do it?

Why use R to do it?

Why use R to do it?

Few lines of codes

library(raster)
library(mapview)
elvBtn = getData('alt', country='BTN', path='./assets/')
mapview(elvBtn)

Why use R to do it?

Few lines of codes

Why use R to do it?

A large community

Why use R to do it?

Common plots used in meta-analyses

What do we want/need to visualize?

Effect Size (ES)

  • log risk ratios,
  • log odds ratios,
  • …,
  • non-standard ES

Studies

  • by author
  • by region
  • by species

Effect size – Funnel plot

Effect size – Forest plot









Vellend et. al 2013

Effect size – Forest plot

Effect size – Radial plot

Effect size – L'Abbé plot

Number of Study & effect size

Vellend et al. 2013

Number of Study & geography


Vellend et al. 2013

Common plots used in meta-analyses with R

R packages for meta-analyses

  • click here kev

  • install.packages("ctv"); ctv::install.views("MetaAnalysis")

Meta vs Metafor – briefly

Meta vs Metafor – briefly

Meta vs Metafor – support

Meta – workflow

  1. meta:
    • metabin()
Arguments:

 event.e: Number of events in experimental group.
     n.e: Number of observations in experimental group.
 event.c: Number of events in control group.
     n.c: Number of observations in control group.

Meta – workflow

meta:

- `metabin()` / `metagen()`
Arguments:

      TE: Estimate of treatment effect.
    seTE: Standard error of treatment estimate.

Meta – workflow

meta:

- `metabin()` / `metagen()`
- `funnel`, `forest`, ..
install.packages('meta')
library(meta)
data(Olkin95)
# ?Olkin95
meta1 <- metabin(event.e, n.e, event.c, n.c,
                  data=Olkin95, subset=c(41,47,51,59),
                  sm="RR", method="I",
                  studlab=paste(author, year))

forest(meta1)

Meta – workflow

author year event.e n.e event.c n.c
Fletcher 1959 1 12 4 11
Dewar 1963 4 21 7 21
Lippschutz 1965 6 43 7 41
European 1 1969 20 83 15 84
European 2 1971 69 373 94 357
Heikinheimo 1971 22 219 17 207

Meta – workflow

Metafor – workflow

Metafor – workflow

  • Calculate effect size: escalc()

  • Perform analysis: rma()

  • Plots: funnel(), 'forest()', 'labbe()', …

Metafor – workflow

install.packages('metafor')
library('metafor')

Metafor – example

r<-c(0.5,0.6,0.4,0.2,0.7,0.45); n<-c(40,90,25,400,60,50);
studynames<-c(1,2,3,4,5,6); b<-data.frame(r,n,studynames)
eszcor <- escalc(measure="ZCOR", ri=r, ni=n, data=b)
FEmodel<-rma(yi=yi, vi=vi, data=eszcor, method="FE")
#forest plot fixed-effect model: pooled effect size,
forest(FEmodel); forest(FEmodel,transf=transf.ztor)

Funnel plot – version 2

Funnel plot – version 3

Forest plot

Pros and Cons

Pros

  • do everything for you
  • good-looking plots with a few line of plot

Cons

  • standard ES
  • not fully customizable

Custom your plot

graphics or grid?

Few tips

  • par()
  • plot(), barplot(), funnel()

npt <- 15
vecX <- 1:npt
vecY <- runif(length(vecX))

plot(vecX, vecY)

plot(vecX, vecY, xlim=c(0,15)+.5)

plot(vecX, vecY, xlim=c(0,15)+.5, pch=19)

plot(vecX, vecY, xlim=c(0,15)+.5, pch=19, col="#0f7aa2")

plot(vecX, vecY, xlim=c(0,15)+.5, pch='S', col="#0f7aa2")

plot(vecX, vecY, xlim=c(0,15)+.5, type='h', col="#0f7aa2")

plot(vecX, vecY, xlim=c(0,15)+.5, type='h', col="#0f7aa2", lwd=12)

par(lend=2); plot(vecX, vecY, xlim=c(0,15)+.5, type='h', col="#0f7aa2", lwd=12)

par(las=1)
plot(vecX, vecY, xlim=c(0,15)+.5, pch=19, col="#0f7aa2")

par(las=1)
plot(vecX, vecY, xlim=c(0,15)+.5, pch=19, col="#0f7aa2")
abline(v=c(0.5,15.5), lty=2)

par(las=1, xaxs='i')
plot(vecX, vecY, xlim=c(0,15)+.5, pch=19, col="#0f7aa2")
abline(v=c(0.5,15.5), lty=2)

vCol <- rep(1, npt)
vCol[c(2,5,8,9)] <- 2
vSize <- 1+2*runif(npt)

par(las=1, xaxs='i')
plot(vecX, vecY, xlim=c(0,15)+.5, pch=19, col=c("#0f7aa2", '#a70d72')[vCol])

par(las=1, xaxs='i')
plot(vecX, vecY, xlim=c(0,15)+.5, pch=19, col=c("#0f7aa2", '#a70d72')[vCol], cex=vSize)

par(las=1, xaxs='i', mfrow=c(2,2))
for (i in 1:4){
  plot(vecX, vecY, xlim=c(0,15)+.5, pch=19, col=c("#0f7aa2", '#a70d72')[vCol], cex=vSize)
}

par(las=1, xaxs='i', mfrow=c(2,2), mar=c(2,2,1,1), oma=c(2,2,0,0))
##--
plot(vecX, vecY, xlim=c(0,15)+.5, pch=19, col=c("#0f7aa2", '#a70d72')[vCol], cex=vSize,
ann=F)
##--
plot(vecX, vecY, xlim=c(0,15)+.5, pch=19, col=c("#0f7aa2", '#a70d72')[vCol], cex=vSize,
ann=F)
##--
plot(vecX, vecY, xlim=c(0,15)+.5, pch=19, col=c("#0f7aa2", '#a70d72')[vCol], cex=vSize,
ann=F)
##--
plot(vecX, vecY, xlim=c(0,15)+.5, pch=19, col=c("#0f7aa2", '#a70d72')[vCol], cex=vSize,
ann=F)
##--
mtext(1, text='Time', outer=T)
par(las=0)
mtext(2, text='Variable', outer=T)

Example funnel plot

Questions?

Let's practice

Reproduce 2 figures from Vellend et al. 2013



  • Paper is available on line

  • Data available on line as well Dataset S1

Figure S2

Vellend et al. 2013

Figure 3









Extra

WordCloud

WordCloud

Network of co-authors

igraph

Resources

Useful links

That's all folks